#include "../../h/apue.h"
#include <fcntl.h>

#define BUFFSIZE 4096

int main(void)
{
    //int val;

    int a = open("./a.txt", O_RDONLY);
    int b = open("./a.txt", O_WRONLY);

    printf("a=%d, b= %d.\n", a, b);

    return 0;
}

void set_fl(int fd, int flags)
{
    int val;

    if ((val = fcntl(fd, F_GETFL, 0)) < 0)
        err_sys("fcntl F_GETFL error");

    val |= flags;

    if (fcntl(fd, F_SETFL, val) < 0)
        err_sys("fcntl F_SETFL error");

}

void clr_fl(int fd, int flags)
{
    int val;
    if ((val = fcntl(fd, F_GETFL, 0)) < 0)
        err_sys("fcntl F_GETFL error");

    val &= ~flags;
    if (fcntl(fd, F_SETFL, val) < 0)
        err_sys("fcntl F_SETFL error");
}

int dup2_2(int fd, int fd2)
{
    int i = 3;
    char buf[1];

    if (fd == fd2)
        return fd;

    close(fd2);
    for (i; i<fd2; ++i)
        if (read(i, buf, 1) == -1)
            dup(fd);

    return dup(fd);
}
